/**
 *
 */
package com.zhuoyue.zhtj.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import com.google.common.collect.Lists;
import com.zhuoyue.zhtj.common.Consts;
import com.zhuoyue.zhtj.entity.LiveVo3;
import com.zhuoyue.zhtj.sdk.api.LetvApiClient;
import com.zhuoyue.elmt.utils.GsonUtils;
import com.zhuoyue.zhtj.entity.LiveVo;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.eit.eitsite.common.persistence.Page;
import com.eit.eitsite.common.service.CrudService;
import com.zhuoyue.zhtj.entity.Zbxx;
import com.zhuoyue.zhtj.dao.ZbxxDao;

/**
 * 直播信息Service
 * @author hss
 * @version 2017-08-17
 */
@Service
@Transactional(readOnly = true)
public class ZbxxService extends CrudService<ZbxxDao, Zbxx> {
	@Autowired
	private ZbxxDao zbxxDao;

	public Zbxx get(String id) {
		return super.get(id);
	}
	
	public List<Zbxx> findList(Zbxx zbxx) {
		return super.findList(zbxx);
	}
	
	public Page<Zbxx> findPage(Page<Zbxx> page, Zbxx zbxx) {
		Page<Zbxx> pages=super.findPage(page, zbxx);
		for(Zbxx zbxxs:pages.getList()){
			if(zbxxs.getZbtp()!=null&&zbxxs.getZbtp().indexOf(";")!=-1){
			zbxxs.setZbtp(zbxxs.getZbtp().split(";")[0]);
			}
		}
		return pages;
	}

	public Zbxx findByActivityId(LiveVo3 vo3){
		if(vo3.getActivityId()!=null){
			Zbxx zbxx=zbxxDao.findByActivityId(vo3);
			return zbxx;
		}
		return null;
	}

	@Transactional(readOnly = false)
	public void delete(Zbxx zbxx) {
		super.delete(zbxx);
	}

	public LiveVo getLive(String offSet,String fetchSize,LiveVo3 liveVo3){
		LetvApiClient tv =LetvApiClient.getInstance();
		tv.setApiUrl("http://api.open.lecloud.com/live/execute");
		tv.setUserid(Consts.userid);
		tv.setSecret(Consts.userSecret);
		try {
			tv.initialize();
			Map<String,String> map=new HashMap<String,String>();
			map.put("offSet",offSet==null? Consts.offSet:offSet);
			map.put("fetchSize",fetchSize==null?Consts.fetchSize:fetchSize);
			if(liveVo3!=null){
				String id=liveVo3.getActivityId();
				String activityName=liveVo3.getActivityName();
				String activityStatus=liveVo3.getActivityStatus();
				if(id!=null&&!"".equals(id)){
					map.put("activityId",id);
				}
				if(activityName!=null&&!"".equals(activityName)){
					map.put("activityName",activityName);
				}
				if(activityStatus!=null&&!"".equals(activityStatus)){
					map.put("activityStatus",activityStatus);
				}
			}
			HttpResponse hr=tv.executeGet("lecloud.cloudlive.activity.search","4.1",map);
			String data= IOUtils.toString(hr.getEntity().getContent());
			LiveVo liveVo=GsonUtils.newInstance().fromJson(data,LiveVo.class);
			return liveVo;
			}catch (Exception e){
			e.printStackTrace();
		}
		return null;
	}

	public LiveVo getPartInfo(){
		LiveVo liveVo=getLive(null,"20",null);
		Random random=new Random();
		List<LiveVo3> list=Lists.newArrayList();
		if(liveVo!=null){
			int row=liveVo.getRows().size();
			if(row>3){
				for (int i=0;i<3;i++){
					list.add(liveVo.getRows().get(random.nextInt(row)));
				}
				liveVo.setRows(list);
			}
		}
		return liveVo;
	}
}